<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="70px">
      <el-form-item label="SOA订单号" prop="orderCode" label-width="100px">
        <el-input
          v-model="queryParams.orderCode"
          placeholder="请输入SOA订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="套餐编码" prop="packageCode">
        <el-input
          v-model="queryParams.packageCode"
          placeholder="请输入套餐编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="套餐名称" prop="packageName">
        <el-input
          v-model="queryParams.packageName"
          placeholder="请输入套餐名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="有效期始" prop="startDate">
        <el-date-picker clearable
                        v-model="queryParams.startDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择有效期始">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="有效期止" prop="endDate">
        <el-date-picker clearable
                        v-model="queryParams.endDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择有效期止">
        </el-date-picker>
      </el-form-item>
<!--      <el-form-item label="在岗状态" prop="checkTypeEnum">-->
<!--        <el-select v-model="queryParams.worktype" placeholder="请选择在岗状态" clearable>-->
<!--          <el-option-->
<!--            v-for="dict in dict.type.dime_physical_work"-->
<!--            :key="dict.value"-->
<!--            :label="dict.label"-->
<!--            :value="dict.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="价格" prop="packageOriginalPrice">-->
<!--        <el-input-->
<!--          v-model="queryParams.packageOriginalPrice"-->
<!--          placeholder="请输入价格"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="折扣价" prop="packageDiscountPrice">-->
<!--        <el-input-->
<!--          v-model="queryParams.packageDiscountPrice"-->
<!--          placeholder="请输入折扣价"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="适用性别" prop="sexEnum">-->
<!--        <el-select v-model="queryParams.sexEnum" placeholder="请选择适用性别" clearable>-->
<!--          <el-option-->
<!--            v-for="dict in dict.type.sys_user_sex"-->
<!--            :key="dict.value"-->
<!--            :label="dict.label"-->
<!--            :value="dict.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="婚否" prop="marriageTag">-->
<!--        <el-select v-model="queryParams.marriageTag" placeholder="请选择婚否" clearable>-->
<!--          <el-option-->
<!--            v-for="dict in dict.type.dime_physical_marrie"-->
<!--            :key="dict.value"-->
<!--            :label="dict.label"-->
<!--            :value="dict.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="启用" prop="isValid">
<!--        <el-radio-group v-model="queryParams.isValid">-->
<!--          <el-radio :label="1">是</el-radio>-->
<!--          <el-radio :label="0">否</el-radio>-->
<!--        </el-radio-group>-->
        <el-switch v-model="queryParams.isValid"
                   active-color="#13ce66"
                   inactive-color="#ff4949"
                   :active-value="1"
                   :inactive-value="0">
        </el-switch>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['physical:basepackage:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['physical:basepackage:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['physical:basepackage:remove']"
        >禁用</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['physical:basepackage:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="basepackageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="SOA订单号" width="200" align="center" prop="orderCode" />
      <el-table-column label="套餐编码" width="200" align="center" prop="packageCode" />
      <el-table-column label="套餐名称" width="400" align="center" prop="packageName" />
      <el-table-column label="在岗状态" align="center" prop="worktype">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.dime_physical_work" :value="scope.row.worktype"/>
        </template>
      </el-table-column>
      <el-table-column label="有效期始" align="center" prop="startDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="有效期止" align="center" prop="endDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="启用" align="center" >
<!--        <template slot-scope="scope">{{-->
<!--            scope.row.isValid ? "是" : "否"-->
<!--          }}</template>-->
        <template slot-scope="scope">
          <el-switch v-model="scope.row.isValid"
                     active-color="#13ce66"
                     inactive-color="#ff4949"
                     :active-value="1"
                     :inactive-value="0"
                     disabled>
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['physical:basepackage:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['physical:basepackage:remove']"
          >禁用</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改企业套餐管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="套餐编码" prop="packageCode">
              <el-input v-model="form.packageCode" placeholder="请输入套餐编码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="套餐名称" prop="packageName">
              <el-input v-model="form.packageName" placeholder="请输入套餐名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="在岗状态" prop="worktype">
              <el-select v-model="form.worktype" placeholder="请选择在岗状态" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.dime_physical_work"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属单位" prop="companyNameId">
              <el-select v-model="form.companyNameId" filterable placeholder="请选择所属单位" style="width: 100%">
                <el-option
                  v-for="item in basecompanyList"
                  :key="item.id"
                  :label="item.companyName"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="价格" prop="packageOriginalPrice">
              <el-input-number v-model="form.packageOriginalPrice" :min="0" label="请输入原价" style="width: 100%"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="折扣价" prop="packageDiscountPrice">
              <el-input-number v-model="form.packageDiscountPrice" :min="0" label="请输入协议价" style="width: 100%"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="适用性别" prop="sexEnum">
              <el-select v-model="form.sexEnum" placeholder="请选择适用性别" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.sys_user_sex"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="婚否" prop="marriageTag">
              <el-select v-model="form.marriageTag" placeholder="请选择婚否" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.dime_physical_marrie"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="有效期始" prop="startDate">
              <el-date-picker clearable
                              style="width: 100%"
                              v-model="form.startDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择有效期始">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="有效期止" prop="endDate">
              <el-date-picker clearable
                              style="width: 100%"
                              v-model="form.endDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择有效期止">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="套餐描述" prop="description">
            <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
          </el-form-item>
        </el-row>
        <el-row :gutter="10">
<!--          <el-col :span="12">-->
<!--            <el-form-item label="是否公用" prop="publicTag">-->
<!--                <el-radio-group v-model="form.publicTag">-->
<!--                  <el-radio :label="1">是</el-radio>-->
<!--                  <el-radio :label="0">否</el-radio>-->
<!--                </el-radio-group>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
            <el-col :span="12">
              <el-form-item label="启用" prop="isValid">
<!--                <el-radio-group v-model="form.isValid">-->
<!--                  <el-radio :label="1">是</el-radio>-->
<!--                  <el-radio :label="0">否</el-radio>-->
<!--                </el-radio-group>-->
                <el-switch v-model="form.isValid"
                           active-color="#13ce66"
                           inactive-color="#ff4949"
                           :active-value="1"
                           :inactive-value="0">
                </el-switch>
              </el-form-item>
            </el-col>
          </el-row>
        <el-row>
          <el-form-item label="套餐项目">
            <el-transfer
              filterable
              :titles="['待选择', '已选择']"
              filter-placeholder="关键词搜索"
              v-model="form.physicalCombDtoIdList"
              :data="basecombList">
            </el-transfer>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="套餐危害">
            <el-transfer
              filterable
              :titles="['待选择', '已选择']"
              filter-placeholder="关键词搜索"
              v-model="form.dimeBaseharmIdList"
              :data="baseharmList">
            </el-transfer>
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBasepackage, getBasepackage, delBasepackage, addBasepackage, updateBasepackage } from "@/api/physical/basepackage";
import {getBasecomb, listBasecomb} from '@/api/basic/basecomb'
import { listBaseharm } from '@/api/basic/baseharm'
import { listBasecompany } from '@/api/physical/basecompany'

export default {
  name: "Basepackage",
  dicts: ['sys_user_sex', 'dime_physical_marrie', 'dime_physical_work'],
  data() {
    // 验证时间先后
    var checkDate = (rule, value, callback) => {
      if(new Date(this.form.endDate) - new Date(this.form.startDate) < 0 || !this.form.startDate || !this.form.endDate) {
        return callback(new Error('请选择正确有效期'))
      }
      return callback()
    };
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业套餐管理表格数据
      basepackageList: [],
      //项目组合列表
      basecombList: [],
      //危害列表
      baseharmList: [],
      //单位列表
      basecompanyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        packageCode: null,
        orderCode: null,
        packageName: null,
        checkTypeEnum: null,
        packageOriginalPrice: null,
        packageDiscountPrice: null,
        sexEnum: null,
        marriageTag: null,
        startDate: null,
        endDate: null,
        isValid: 1
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        packageCode: [
          { required: true, message: "套餐编码不能为空", trigger: "blur" }
        ],
        packageName: [
          { required: true, message: "套餐名称不能为空", trigger: "blur" }
        ],
        startDate: [
          { required: true, message: "有效期始不能为空", trigger: "blur" },
          { validator: checkDate, trigger: "blur" }
        ],
        endDate: [
          { required: true, message: "有效期止不能为空", trigger: "blur" },
          { validator: checkDate, trigger: "blur" }
        ],
        // worktype: [
        //   { required: true, message: "在岗状态不能为空", trigger: "change" }
        // ],
        companyNameId: [
          { required: true, message: "所属单位不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
    listBasecomb({pageNum: 1,
      pageSize: 100}).then(response => {
      this.basecombList = response.rows;
      this.basecombList.forEach(item => {
        item.key = item.id
        item.label = item.combName
      })
    });
    listBasecompany({pageNum: 1,
      pageSize: 100}).then(response => {
      this.basecompanyList = response.rows;
    });
    listBaseharm({pageNum: 1,
      pageSize: 600}).then(response => {
      this.baseharmList = response.rows;
      this.baseharmList.forEach(item => {
        item.key = item.id
        item.label = item.hamName
      })
    });
  },
  methods: {
    /** 查询企业套餐管理列表 */
    getList() {
      this.loading = true;
      listBasepackage(this.queryParams).then(response => {
        this.basepackageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        packageCode: null,
        packageName: null,
        checkTypeEnum: null,
        companyNameId: null,
        packageOriginalPrice: null,
        packageDiscountPrice: null,
        sexEnum: null,
        marriageTag: null,
        startDate: null,
        endDate: null,
        examineTag: null,
        examiner: null,
        examineDate: null,
        publicTag: null,
        description: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        IsValid: null,
        shopCode: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.form.isValid = 1;
      this.title = "添加企业套餐管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBasepackage(id).then(response => {
        if (response.data.physicalCombDtoList) {
          response.data.physicalCombDtoIdList = response.data.physicalCombDtoList.map(item => {
            return item.id
          })
        }
        if (response.data.dimeBaseharmList) {
          response.data.dimeBaseharmIdList = response.data.dimeBaseharmList.map(item => {
            return item.id
          })
        }
        this.form = response.data;
        this.open = true;
        this.title = "修改企业套餐管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        this.form.physicalCombDtoList = this.physicalCombDtoList;
        this.form.dimeBaseharmList = this.dimeBaseharmList;
        if (valid) {
          if (this.form.physicalCombDtoIdList && this.form.physicalCombDtoIdList.length > 0) {
            this.form.physicalCombDtoList = this.form.physicalCombDtoIdList.map(id => {
              return {
                id: id
              }
            })
          }
          if (this.form.dimeBaseharmIdList && this.form.dimeBaseharmIdList.length > 0) {
            this.form.dimeBaseharmList = this.form.dimeBaseharmIdList.map(id => {
              return {
                id: id
              }
            })
          }
          if (this.form.id != null) {
            updateBasepackage(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBasepackage(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 禁用按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认禁用企业套餐管理编号为"' + ids + '"的数据项？').then(function() {
        return delBasepackage(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("禁用成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('physical/basepackage/export', {
        ...this.queryParams
      }, `basepackage_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
